package com.ruoyi.web.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.config.pay.CommonFieldName;
import com.ruoyi.common.config.pay.OrderIdGenerator;
import com.ruoyi.web.request.IESI123101Request;
import com.ruoyi.web.service.AgentPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.PaymentLimitApplyMapper;
import com.ruoyi.web.domain.PaymentLimitApply;
import com.ruoyi.web.service.IPaymentLimitApplyService;
import com.ruoyi.common.core.text.Convert;

/**
 * 代付限额申请Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-03
 */
@Service
public class PaymentLimitApplyServiceImpl implements IPaymentLimitApplyService 
{
    @Autowired
    private PaymentLimitApplyMapper paymentLimitApplyMapper;
    @Autowired
    private AgentPayService agentPayService;

    /**
     * 查询代付限额申请
     * 
     * @param sequenceNo 代付限额申请ID
     * @return 代付限额申请
     */
    @Override
    public PaymentLimitApply selectPaymentLimitApplyById(String sequenceNo)
    {
        return paymentLimitApplyMapper.selectPaymentLimitApplyById(sequenceNo);
    }

    /**
     * 查询代付限额申请列表
     * 
     * @param paymentLimitApply 代付限额申请
     * @return 代付限额申请
     */
    @Override
    public List<PaymentLimitApply> selectPaymentLimitApplyList(PaymentLimitApply paymentLimitApply)
    {
        return paymentLimitApplyMapper.selectPaymentLimitApplyList(paymentLimitApply);
    }

    /**
     * 新增代付限额申请
     * 
     * @param paymentLimitApply 代付限额申请
     * @return 结果
     */
    @Override
    public int insertPaymentLimitApply(PaymentLimitApply paymentLimitApply) throws Exception
    {
        IESI123101Request request =new IESI123101Request();
        request.setAmt(paymentLimitApply.getAmt().setScale(2));
        request.setAddInfo(paymentLimitApply.getAddInfo());
        request.setMode(paymentLimitApply.getMode());
        request.setType(paymentLimitApply.getType());
        if (paymentLimitApply.getCnt()!=null){
            request.setCnt(new BigDecimal(paymentLimitApply.getCnt()).setScale(2));
        }else {
            request.setCnt(new BigDecimal(1).setScale(2));
        }

        request.setFileId(paymentLimitApply.getFileId());
        request.setFileName(paymentLimitApply.getFileName());
        paymentLimitApply.setSequenceNo(OrderIdGenerator.getDefault().nextId());
        HashMap<String, Object> resultMap= agentPayService.IESI123101(request,paymentLimitApply.getSequenceNo(),paymentLimitApply.getMerchCd());
        String respCode = (String) resultMap.get(CommonFieldName.RESP_CODE);
        String respMsg = (String) resultMap.get(CommonFieldName.RESP_MSG);
        paymentLimitApply.setStatus(respCode);
        paymentLimitApply.setMsg(respMsg);
        return paymentLimitApplyMapper.insertPaymentLimitApply(paymentLimitApply);
    }

    /**
     * 修改代付限额申请
     * 
     * @param paymentLimitApply 代付限额申请
     * @return 结果
     */
    @Override
    public int updatePaymentLimitApply(PaymentLimitApply paymentLimitApply)
    {
        return paymentLimitApplyMapper.updatePaymentLimitApply(paymentLimitApply);
    }

    /**
     * 删除代付限额申请对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePaymentLimitApplyByIds(String ids)
    {
        return paymentLimitApplyMapper.deletePaymentLimitApplyByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除代付限额申请信息
     * 
     * @param sequenceNo 代付限额申请ID
     * @return 结果
     */
    @Override
    public int deletePaymentLimitApplyById(String sequenceNo)
    {
        return paymentLimitApplyMapper.deletePaymentLimitApplyById(sequenceNo);
    }
}
